<?php
// +----------------------------------------------------------------------
// | ThinkPHP
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$

class Gather extends Base {

    // 获取所有的连接
    static function getAllUrl($code){
        preg_match_all('/<a\s+[\w]?href=["|\']?([^>"\' ]+)["|\']?\s*[^>]*>([^>]+)<\/a>/i',$code,$arr);
        return array('name'=>$arr[2],'url'=>$arr[1]);
    }

    // 获取所有的图像
    static function getAllImages($code,$distinct=false) {
        preg_match_all ("/<img[^<]+src=['\"\s]*([^'\"\s]+\.(jpg|gif|png|bmp))['\"\s]+/i", $code, $match);
        if ($distinct == true) {
                $match[1] = array_unique($match[1]);
        }
        return $match[1];
    }

    static function getMatchData($data,$pattern) {
        $result   =  preg_match_all('/'.$pattern.'/i',$data,$match);
        if($result) {
            return $match[1];
        }else{
            return false;
        }
    }
    static function getUrl($url,$timeout=0) {
        $ch   =  curl_init($url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

    //获取指定标记中的内容
    static function getTagData($str, $start, $end){
        if ( $start == '' || $end == '' ){
               return;
        }
        $str = explode($start, $str);
        $str = explode($end, $str[1]);
        return $str[0];
    }

    //HTML表格的每行转为CSV格式数组
    function get_tr_array($table) {
        $table = preg_replace("'<td[^>]*?>'si",'"',$table);
        $table = str_replace("</td>",'",',$table);
        $table = str_replace("</tr>","{tr}",$table);
        //去掉 HTML 标记
        $table = preg_replace("'<[\/\!]*?[^<>]*?>'si","",$table);
        //去掉空白字符
        $table = preg_replace("'([\r\n])[\s]+'","",$table);
        $table = str_replace(" ","",$table);
        $table = str_replace(" ","",$table);

        $table = explode(",{tr}",$table);
        array_pop($table);
        return $table;
    }
    //将HTML表格的每行每列转为数组，采集表格数据
    function get_td_array($table) {
        $table = preg_replace("'<table[^>]*?>'si","",$table);
        $table = preg_replace("'<tr[^>]*?>'si","",$table);
        $table = preg_replace("'<td[^>]*?>'si","",$table);
        $table = str_replace("</tr>","{tr}",$table);
        $table = str_replace("</td>","{td}",$table);
        //去掉 HTML 标记
        $table = preg_replace("'<[\/\!]*?[^<>]*?>'si","",$table);
        //去掉空白字符
        $table = preg_replace("'([\r\n])[\s]+'","",$table);
        $table = str_replace(" ","",$table);
        $table = str_replace(" ","",$table);

        $table = explode('{tr}', $table);
        array_pop($table);
        foreach ($table as $key=>$tr) {
                $td = explode('{td}', $tr);
                array_pop($td);
            $td_array[] = $td;
        }
        return $td_array;
    }

    //返回字符串中的所有单词 $distinct=true 去除重复
    function split_en_str($str,$distinct=true) {
        preg_match_all('/([a-zA-Z]+)/',$str,$match);
        if ($distinct == true) {
                $match[1] = array_unique($match[1]);
        }
        sort($match[1]);
        return $match[1];
    }

    //相对路径转化成绝对路径
    function relative_to_absolute($content, $feed_url) {
        preg_match('/(http|https|ftp):\/\//', $feed_url, $protocol);
        $server_url = preg_replace("/(http|https|ftp|news):\/\//", "", $feed_url);
        $server_url = preg_replace("/\/.*/", "", $server_url);

        if ($server_url == '') {
            return $content;
        }

        if (isset($protocol[0])) {
            $new_content = preg_replace('/href="\//', 'href="'.$protocol[0].$server_url.'/', $content);
            $new_content = preg_replace('/src="\//', 'src="'.$protocol[0].$server_url.'/', $new_content);
        } else {
            $new_content = $content;
        }
        return $new_content;
    }

    /**
     +----------------------------------------------------------
     * 下载远程文件到本地
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $remote 远程文件名
     * @param string $local 本地保存文件名
     +----------------------------------------------------------
     * @return mixed
     +----------------------------------------------------------
     */
    static public function download($remote,$local) {
        $cp = curl_init($remote);
        $fp = fopen($local,"w");
        $url   =  parse_url($remote);
        curl_setopt($cp, CURLOPT_REFERER, $url['scheme'].'://'.$url['host']);
        curl_setopt($cp, CURLOPT_FILE, $fp);
        curl_setopt($cp, CURLOPT_HEADER, 0);
        curl_setopt($cp,CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)');
        curl_exec($cp);
        curl_close($cp);
        fclose($fp);
    }

    static public function doRequest($url, $method='GET',$vars='',$callback='') {
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt($ch, CURLOPT_HEADER, 1);
       curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
       curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
       if ($method == 'POST') {
           curl_setopt($ch, CURLOPT_POST, 1);
           curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
       }
       $data = curl_exec($ch);
       curl_close($ch);
       if ($data) {
           if ($callback)
           {
               return call_user_func($callback, $data);
           } else {
               return $data;
           }
       } else {
           return curl_error($ch);
       }
    }

}
?>